package org.vaadin.addon.markdownlabel.example;

import org.vaadin.addon.markdownlabel.MarkdownLabel;

import com.vaadin.Application;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.Window;

public class MarkdownlabelApplication extends Application implements TextChangeListener {
	
	private MarkdownLabel markdownLabel;
	
	@Override
	public void init() {
		HorizontalLayout rootLayout = new HorizontalLayout();
		rootLayout.setSizeFull();
		Window mainWindow = new Window("Markdownforvaadin Application");
		mainWindow.setContent(rootLayout);
		setMainWindow(mainWindow);
		
		String markdown = getExampleText();
		
		Panel normal = new Panel("Markdown syntax");
		TextArea editor = new TextArea(null, markdown);
		editor.setRows(30);
		editor.setColumns(20);
		editor.addListener(this);
		editor.setImmediate(true);
		normal.addComponent(editor);
		mainWindow.addComponent(normal);
		
		Panel processed = new Panel("Processed markdown syntax");
		markdownLabel = new MarkdownLabel();
		markdownLabel.setValue(markdown);
		processed.addComponent(markdownLabel);
		mainWindow.addComponent(processed);
	}

	public void textChange(TextChangeEvent event) {
		String editorText = event.getText();
		markdownLabel.setValue(editorText);
	}
	
	private String getExampleText() {
		StringBuffer sb = new StringBuffer();
		
		sb.append("Syntax examples\n=============\n\n");
		sb.append("## List\n");
		sb.append("1. Item\n");
		sb.append("2. Stylish text\n");
		sb.append("3. Item with *emphasized* text\n");
		sb.append("4. **This is really _important_**\n");
		sb.append("\n");
		sb.append("[Markdown](http://daringfireball.net/projects/markdown) is cool!\n");
		sb.append("\n***\n");
		sb.append("Normal text\n\n");
		sb.append("\tCode block for special people.\n");
		sb.append("\n");
		sb.append("![Vaadin rocks!](http://vaadin.com/vaadin-theme/images/vaadin/vaadin-logo-white.png \"Vaadin alt text\")\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("\n");
		sb.append("\n");
		
		return sb.toString();
	}

}
